﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;
using System.Collections.Generic;
using Nova_BLL;

public partial class System_User_UserList : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    Nova_MOD.Users Nova_MOD_Users = new Nova_MOD.Users();
    Nova_BLL.Users Nova_BLL_Users = new Nova_BLL.Users();
    TUserMaterialGroupRelationBll UserGroupRelationBll = new TUserMaterialGroupRelationBll();
    protected int iCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();

        if (!IsPostBack)
        {
            gv_Bind();
        }
        if (hidden.Value != "")
        {
            gv_Bind();
        }
        hidden.Value = "";

    }

    /// <summary>
    /// 工作组列表
    /// </summary>
    protected void gv_Bind()
    {
        ds = Nova_BLL_Users.GetAllUserDataSet();
        gvUser.DataSource = ds;
        gvUser.DataBind();
    }



    /// <summary>
    /// 清空
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtvUserNo.Text = "";
        txtvUserName.Text = "";
        txtvDept.Text = "";
    }


    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDel_Click(object sender, ImageClickEventArgs e)
    {
        CheckBox cbx = null;
        StringBuilder listId = new StringBuilder();
        string userNo = string.Empty;
        for (int i = 0; i < gvUser.Rows.Count; i++)
        {
            cbx = (CheckBox)gvUser.Rows[i].Cells[0].FindControl("chkSelect");
            if (cbx.Checked)
            {
                listId.Append("'" + gvUser.DataKeys[i].Value.ToString() + "',");
                LinkButton lb_UserNo = (LinkButton)gvUser.Rows[i].FindControl("lbtn_vUserNo");
                userNo = lb_UserNo.Text;
            }
        }

        if (listId.Length > 0)
        {
            try
            {
                iCount = Nova_BLL_Users.DeleteUsers(listId.ToString().TrimEnd(','));
                UserGroupRelationBll.DeleteByUserNo(userNo);
                if (iCount > 0)
                {
                    ShowBox.MessageBox(this.Page, "删除成功!");
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        else
        {
            ShowBox.MessageBox(this.Page, "请选择勾选需要删除的用户!");
        }
    }

    /// <summary>
    /// 查询(使用的是模糊查询)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnQuery_Click(object sender, ImageClickEventArgs e)
    {
       
        ds = Nova_BLL_Users.GetUserDataSet(txtvUserNo.Text.ToString().Trim(), txtvUserName.Text.ToString().Trim(), txtvDept.Text.ToString().Trim());
        gvUser.DataSource = ds;
        gvUser.DataBind();
    }
    /// <summary>
    /// 绑定gv触发事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lbtn_vUserNo = (LinkButton)e.Row.FindControl("lbtn_vUserNo");

            string str = string.Format("javascript:document.getElementById('{3}').value=window.open('UserListAdd.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&Mode={0}&UserNo={1}&id={2}', window, 'Height=230px,Width=600px,top=250, left=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');document.URL=location.href;void(0);",
            "Update", lbtn_vUserNo.CommandArgument.ToString(), gvUser.DataKeys[e.Row.RowIndex].Value.ToString(), hidden.ClientID);
            lbtn_vUserNo.Attributes.Add("onclick", str);

            DataRowView dv = (DataRowView)e.Row.DataItem;
            e.Row.Cells[5].Text = (Convert.ToBoolean(dv["iUserStatus"]) ? "是" : "否");
        }
    }

    /// <summary>
    /// 注销
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        CheckBox cbx = null;
        StringBuilder listId = new StringBuilder();  //字符序列
        for (int i = 0; i < gvUser.Rows.Count; i++)
        {
            cbx = (CheckBox)gvUser.Rows[i].Cells[0].FindControl("chkSelect");
            if (cbx.Checked)
            {
                listId.Append("'" + gvUser.DataKeys[i].Value.ToString() + "',");

            }
        }

        if (listId.Length > 0)
        {
            try
            {
                iCount = Nova_BLL_Users.LogoutUsers(listId.ToString().TrimEnd(','));
                if (iCount > 0)
                {
                    ShowBox.MessageBox(this.Page, "注销成功!");
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }

    
}
